//
//  EditFooterView.m
//  夺宝吧
//
//  Created by xiaocai on 15/8/18.
//  Copyright (c) 2015年 FangYuan. All rights reserved.
//

#import "EditFooterView.h"
#import "WYShopCarModel.h"
#import "WYAlertView.h"
#import "MJRefresh.h"
@interface EditFooterView ()<UIAlertViewDelegate>
{
    __weak IBOutlet UIButton *selectBtn;
    __weak IBOutlet UILabel *allLab;
    __weak IBOutlet UILabel *countLab;
    __weak IBOutlet UIButton *deleteBtn;
    
    
    
}
@property (nonatomic , assign) BOOL isRemoveAll;

@property (nonatomic , strong) UIAlertView *alertView;

@end
@implementation EditFooterView
- (void)awakeFromNib
{
    deleteBtn.layer.masksToBounds = YES;
    deleteBtn.layer.cornerRadius = 2;
    [deleteBtn setTitleColor:QBB_RED_COLOR forState:UIControlStateNormal];
    deleteBtn.layer.borderColor = QBB_RED_COLOR.CGColor;
    deleteBtn.layer.borderWidth = 1.0;
    [selectBtn setImage:[UIImage imageNamed:@"icon_sel_del_press"] forState:UIControlStateSelected];
    [self addSubview:[WYPublic getFenGexian:0 :0 :WYMWITH :0.5]];
    
    self.isRemoveAll = NO;
}

- (IBAction)selAllBtnOnClick:(id)sender {
    selectBtn.selected = !selectBtn.selected;
    if (selectBtn.selected) {
        allLab.text = @"取消全选";
        for (WYShopCarModel *model in self.father.data) {
            model.selectedFlag = YES;
        }
        self.isRemoveAll = YES;
        
    }else{
        allLab.text = @"全选";
        for (WYShopCarModel *model in self.father.data) {
            model.selectedFlag = NO;
        }
        self.isRemoveAll = NO;
    }
    [self updateCount];
    [self.father.tableView reloadData];
}
//取消选中
- (void)cancelSelect
{
    selectBtn.selected = NO;
}
- (IBAction)delete:(id)sender {
    NSInteger i=0;
    //删除数据库中该产品记录
    for (WYShopCarModel *model in self.father.data) {
        if (model.selectedFlag) {
            i++;
        }
    }
    if (i==0) {
        [WYPublic showMessageWithTitle:nil Msg:@"请选中要删除的商品"];
        return;
    }
    //弹出对话提示
//    WYAlertView *altView = [WYAlertView showInView:self.father.view withTitle:@"温馨提示：" message:@"您确定将选中产品移除购物车吗？" confirmButtonTitle:@"确定" cancelButtonTitle:@"取消"];
//    [altView handleCancel:^{
//    } handleConfirm:^{
//        [self.father cancelEdit];
//        //删除数据库中该产品记录
//        
//        for (WYShopCarModel *model in self.father.data) {
//            if (model.selectedFlag) {
//                WYDataBase *database = [WYDataBase sharedDataBase];
//                [database deletePdcInCarById:[model.car_new_id intValue]];
//            }
//        }
//        if (self.isRemoveAll){
//            [[WYDataBase sharedDataBase]deleteDatabase];
//            self.isRemoveAll = NO;
//        }
//        self.father.data = [[NSMutableArray alloc]initWithArray:[[WYDataBase sharedDataBase]getAllpdcInCar]];
//        [self.father.tableView headerBeginRefreshing];
//        //刷新个数
//        [[WYMainTabBarVC shareMainTaBarVC]updateCountToCar];
//        self.hidden = YES;
//        [self updateCount];
//        }];
//    [altView show];
    
    //系统自带的删除对话框
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"您确定将产品移除购物车吗?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    self.alertView = alertView;
    [alertView show];
}
//确认取消的alertView的代理
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (self.alertView == alertView){
        if (buttonIndex == 1){
            [self.father cancelEdit];
            //删除数据库中该产品记录
            
            for (WYShopCarModel *model in self.father.data) {
                if (model.selectedFlag) {
                    WYDataBase *database = [WYDataBase sharedDataBase];
                    [database deletePdcInCarById:[model.car_new_id intValue]];
                }
            }
            if (self.isRemoveAll){
                [[WYDataBase sharedDataBase]deleteDatabase];
                self.isRemoveAll = NO;
            }
            self.father.data = [[NSMutableArray alloc]initWithArray:[[WYDataBase sharedDataBase]getAllpdcInCar]];
            [self.father.tableView headerBeginRefreshing];
            //刷新个数
            [[WYMainTabBarVC shareMainTaBarVC]updateCountToCar];
            self.hidden = YES;
            [self updateCount];
        }
    }
}
//刷新选中个数
- (void)updateCount
{
    NSInteger i=0;
    //删除数据库中该产品记录
    for (WYShopCarModel *model in self.father.data) {
        if (model.selectedFlag) {
            i++;
        }
    }
    countLab.text = [NSString stringWithFormat:@"共选中了%ld件商品",(unsigned long)i];
    if (i==self.father.data.count) {
        selectBtn.selected = YES;
        allLab.text = @"取消全选";
    }else{
        selectBtn.selected = NO;
        allLab.text = @"全选";
    }
}
@end
